using System;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TeamFoundation;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;
using TeamReview.Command;
using TeamReview.Replay;
using TeamReview.Services;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
using Microsoft.TeamFoundation.VersionControl.Client;
using EnvDTE;

namespace TeamReview
{
	/// <remarks>
	/// I do realize that this isn't really Dependency Injection but I want to
	/// move that way if possible. Right now this is more of a Simple 
	/// resource registry.
	/// </remarks>
	public sealed class DependencyLocater
	{

		private static DependencyLocater _instance = new DependencyLocater();
		private ITFSService _tfsService = new TFSService();
		private EnvDTE.AddIn _addIn;
		private EnvDTE80.DTE2 _visualStudio;
		private TeamFoundationServerExt _tfs;
		private ShowFormCommand _showReplayWindowCommand = new ShowReplayWindowCommand();
		private ShowFormCommand _showAddCodeReviewItemCommand = new CreateWorkItemCommand();
		private ShowDirectionsFormCommand _showDirectionsCommand = new ShowDirectionsFormCommand();
        
		private ReplayLink _replayLink = new ReplayChain();
		private DocumentService _documentService;
		private IWorkItemGridValueProvider _valueProvider;
		private VersionControlExt _versionControl;
		private SolutionEvents _solutionEvents;

		#region Properties

		public static DependencyLocater Instance
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _instance; }
		}


		public VersionControlExt VersionControl{
			[System.Diagnostics.DebuggerStepThrough]
			get{ return _versionControl;}
		}

		public DocumentService DocumentService
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _documentService; }
		}


		public IWorkItemGridValueProvider ValueProvider
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _valueProvider; }
		}


		public ReplayLink ReplayChain
		{
			get { return _replayLink; }
		}

		public ShowFormCommand ShowDirectionsFormCommand
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _showDirectionsCommand; }
		}

		public ShowFormCommand ShowReplayWindowCommand
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _showReplayWindowCommand; }
		}

		public ShowFormCommand ShowAddCodeReviewItemCommand
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _showAddCodeReviewItemCommand; }
		}

		public EnvDTE.AddIn AddIn
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _addIn; }
		}

		public EnvDTE80.DTE2 VisualStudio
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _visualStudio; }
		}

		public ITFSService TFSService
		{
			[System.Diagnostics.DebuggerStepThrough]
			get { return _tfsService; }
		}


		#endregion


		public void Initialize(EnvDTE.AddIn addIn, EnvDTE80.DTE2 visualStudio)
		{
			_addIn = addIn;
			_visualStudio = visualStudio;
			_tfs = (TeamFoundationServerExt)visualStudio.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt");
			_documentService = (DocumentService)visualStudio.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService");
            _solutionEvents = _visualStudio.Events.SolutionEvents;
			_solutionEvents.Opened +=new _dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);
		}

		void SolutionEvents_Opened()
		{
			_versionControl = _visualStudio.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt;
			if (_versionControl == null || _versionControl.SolutionWorkspace == null)
			{
				_tfsService.SetTeamProject(null);
                return;
			}
            _valueProvider = (IWorkItemGridValueProvider)Package.GetGlobalService(typeof(IWorkItemGridValueProvider));
			TeamProject teamProject = _versionControl.SolutionWorkspace.GetTeamProjectForLocalPath(_visualStudio.DTE.Solution.FullName);
			_tfsService.SetTeamProject(teamProject);
		}
	}
}
